package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.ProjectManageDetailRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProjectManageDetailMapper;
import com.ruoyi.system.domain.ProjectManageDetail;
import com.ruoyi.system.service.IProjectManageDetailService;

import javax.annotation.Resource;

/**
 * 项目管理详情Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-01
 */
@Service
public class ProjectManageDetailServiceImpl extends ServiceImpl<ProjectManageDetailMapper, ProjectManageDetail> implements IProjectManageDetailService {
    @Resource
    private ProjectManageDetailMapper projectManageDetailMapper;

    /**
     * 查询项目管理详情
     *
     * @param id 项目管理详情主键
     * @return 项目管理详情
     */
    @Override
    public ProjectManageDetail selectProjectManageDetailById(Long id) {
        return projectManageDetailMapper.selectProjectManageDetailById(id);
    }

    /**
     * 查询项目管理详情列表
     *
     * @param projectManageDetail 项目管理详情
     * @return 项目管理详情
     */
    @Override
    public List<ProjectManageDetail> selectProjectManageDetailList(ProjectManageDetail projectManageDetail) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        List<SysRole> roles = user.getRoles();
        for (SysRole role : roles) {
            if (role.getRoleKey().equals("student")) {
                projectManageDetail.setUserId(user.getUserId());
                break;
            }
        }

        return projectManageDetailMapper.selectProjectManageDetailList(projectManageDetail);
    }

    /**
     * 新增项目管理详情
     *
     * @param projectManageDetail 项目管理详情
     * @return 结果
     */
    @Override
    public int insertProjectManageDetail(ProjectManageDetail projectManageDetail) {
        projectManageDetail.setCreateTime(DateUtils.getNowDate());
        return projectManageDetailMapper.insertProjectManageDetail(projectManageDetail);
    }

    /**
     * 修改项目管理详情
     *
     * @param projectManageDetail 项目管理详情
     * @return 结果
     */
    @Override
    public int updateProjectManageDetail(ProjectManageDetail projectManageDetail) {
        projectManageDetail.setUpdateTime(DateUtils.getNowDate());
        return projectManageDetailMapper.updateProjectManageDetail(projectManageDetail);
    }

    /**
     * 批量删除项目管理详情
     *
     * @param ids 需要删除的项目管理详情主键
     * @return 结果
     */
    @Override
    public int deleteProjectManageDetailByIds(Long[] ids) {
        return projectManageDetailMapper.deleteProjectManageDetailByIds(ids);
    }

    /**
     * 删除项目管理详情信息
     *
     * @param id 项目管理详情主键
     * @return 结果
     */
    @Override
    public int deleteProjectManageDetailById(Long id) {
        return projectManageDetailMapper.deleteProjectManageDetailById(id);
    }

    /**
     * @param projectManageIds
     */
    @Override
    public void deleteProjectManageDetailByProjectManageIds(List<Long> projectManageIds) {
        projectManageDetailMapper.deleteProjectManageDetailByProjectManageId(projectManageIds);
    }

    @Override
    public void submit(Long id) {
        ProjectManageDetail projectManageDetail = projectManageDetailMapper.selectById(id);
        if (projectManageDetail == null) {
            throw new ServiceException("项目详情不存在！");
        }
        //3已完成
        projectManageDetail.setState(3L);
        projectManageDetailMapper.updateById(projectManageDetail);
    }

    @Override
    public void draft(Long id) {
        ProjectManageDetail projectManageDetail = projectManageDetailMapper.selectById(id);
        if (projectManageDetail == null) {
            throw new ServiceException("项目详情不存在！");
        }
        //3已完成
        projectManageDetail.setState(2L);
        projectManageDetailMapper.updateById(projectManageDetail);
    }

    @Override
    public ProjectManageDetailRespVO progress() {
        Long userId = null;
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        List<SysRole> roles = user.getRoles();
        for (SysRole role : roles) {
            if (role.getRoleKey().equals("student")) {
                userId= user.getUserId();
                break;
            }
        }
        // 学生用户看自己的信息
        LambdaQueryWrapper<ProjectManageDetail> lqw = new LambdaQueryWrapper<>();
        if (userId != null){
            lqw.eq(ProjectManageDetail::getUserId,userId);
        }

        lqw.orderByDesc(ProjectManageDetail::getId);
        List<ProjectManageDetail> list = projectManageDetailMapper.selectList(lqw);

        ProjectManageDetailRespVO respVO = new ProjectManageDetailRespVO();
        if (CollUtil.isEmpty(list)) {
            return respVO;
        }

        Map<Long, List<ProjectManageDetail>> listMap = list.stream().collect(Collectors.groupingBy(ProjectManageDetail::getState));
        respVO.setNotCompletedList(listMap.getOrDefault(1L, CollUtil.newArrayList()));
        respVO.setInProgressList(listMap.getOrDefault(2L, CollUtil.newArrayList()));
        respVO.setCompletedList(listMap.getOrDefault(3L, CollUtil.newArrayList()));
        return respVO;
    }
}
